/*
 * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: CC0-1.0
 */

#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "esp_check.h"
#include "bsp/esp-box-3.h"
#include "app_sr.h"
#include "app_sr_handler.h"
#include "esp_afe_sr_iface.h"
#include "app_sr_tts.h"
#include "http_face_match.h"
#include "app_rmker.h"
#include "app_driver.h"
#include "ui/ui.h"

static const char *TAG = "app_sr_handler";

void sr_handler_task(void *pvParam)
{
    QueueHandle_t xQueue = (QueueHandle_t) pvParam;

    while (true) {
        sr_result_t result;
        xQueueReceive(xQueue, &result, portMAX_DELAY);

        ESP_LOGI(TAG, "cmd:%d, wakemode:%d,state:%d", result.command_id, result.wakenet_mode, result.state);

        if (ESP_MN_STATE_TIMEOUT == result.state) {
            ESP_LOGI(TAG, "timeout");
            continue;
        }

        if (WAKENET_DETECTED == result.wakenet_mode) {
            ESP_LOGI(TAG, "wakenet detected");
            app_tts_play("欢迎使用");
            continue;
        }

        if (ESP_MN_STATE_DETECTED & result.state) {
            ESP_LOGI(TAG, "mn detected");

            switch (result.command_id) {
                case 0:
                    ESP_LOGI(TAG, "Open medicine box");
                    app_start_face_check();
                    app_open_box();
                    break;
                case 1:
                    ESP_LOGI(TAG, "Close medicine box");
                    app_id_check_pass();
                    app_close_box();
                    break;
                case 2:
                    ESP_LOGI(TAG, "Start heart rate test");
                    app_HR_check_start();
                    break;
                case 3:
                    ESP_LOGI(TAG, "Show calendar");
                    bsp_display_lock(0);
                    _ui_screen_change(&ui_Screen4, LV_SCR_LOAD_ANIM_FADE_ON, 100, 0, &ui_Screen4_screen_init);
                    bsp_display_unlock();
                    break;
                case 4:
                    ESP_LOGI(TAG, "Show time");
                    bsp_display_lock(0);
                    _ui_screen_change(&ui_Screen2, LV_SCR_LOAD_ANIM_FADE_ON, 100, 0, &ui_Screen2_screen_init);
                    bsp_display_unlock();
                    break;
                case 5:
                    ESP_LOGI(TAG, "Open medicine box one");
                    set_box_state_param(1);
                    app_open_box();
                    break;
                case 6:
                    ESP_LOGI(TAG, "Open medicine box two");
                    set_box_state_param(2);
                    app_open_box();
                    break;
                case 7:
                    ESP_LOGI(TAG, "Open medicine box three");
                    set_box_state_param(3);
                    app_open_box();
                    break;
                case 8:
                    ESP_LOGI(TAG, "Open medicine box four");
                    set_box_state_param(4);
                    app_open_box();
                    break;
                case 9:
                    ESP_LOGI(TAG, "Open medicine box five");
                    set_box_state_param(5);
                    app_open_box();
                    break;
                case 10:
                    ESP_LOGI(TAG, "Open medicine box six");
                    set_box_state_param(6);
                    app_open_box();
                    break;
                case 11:
                    ESP_LOGI(TAG, "Open medicine box seven");
                    set_box_state_param(7);
                    app_open_box();
                    break;
                default:
                    break;
            }
            /* **************** REGISTER COMMAND CALLBACK HERE **************** */
        }
    }

    vTaskDelete(NULL);
}